package com.example.springboot.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springboot.common.*;
import com.example.springboot.entity.Equipment;
import com.example.springboot.entity.EquipmentUpdateDto;
import com.example.springboot.service.EquipmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("/equipment")
public class EquipmentController {
    @Autowired//从spring容器中拿出来
    EquipmentService equipmentService;

    @PostMapping("/add")
    @CcLogs(operation = "设备信息",type = LogType.ADD)
    public Result add(@RequestBody Equipment equipment) {
        try {
            equipmentService.save(equipment);
        } catch (Exception e) {
            if (e instanceof DuplicateKeyException) {
                return Result.error("插入数据错误");
            } else {
                return Result.error("系统错误");
            }
        }
        return Result.success();
    }

    //    修改信息
    @PutMapping("/update")
    @CcLogs(operation = "设备信息",type = LogType.UPDATE)
    public Result update(@RequestBody Equipment equipment) {
        equipmentService.updateById(equipment);
        return Result.success();
    }

    @PutMapping("/update2")
    @CcLogs(operation = "设备信息",type = LogType.UPDATE)
    public ResultCount update2(@RequestBody EquipmentUpdateDto dto) {
        Equipment equipment = equipmentService.getByName(dto.getName());
        BigDecimal currentCount = equipment.getCount();
        BigDecimal useCount = dto.getCount();
        boolean waterSufficient = currentCount.compareTo(useCount) >= 0;
        if (waterSufficient) {
            equipmentService.updateByName(dto.getName(), useCount);
            return ResultCount.success(true);
        }else {
            return ResultCount.success(false);
        }
    }

    //    删除信息
    @DeleteMapping("/delete/{id}")
    @CcLogs(operation = "设备信息",type = LogType.DELETE)
    public Result delete(@PathVariable Integer id) {
        equipmentService.removeById(id);
        return Result.success();
    }

    //    批量删除
    @DeleteMapping("/delete/double")
    @CcLogs(operation = "设备信息",type = LogType.DOUBLEDELETE)
    public Result doubleDelete(@RequestBody List<Integer> ids) {
        equipmentService.removeBatchByIds(ids);
        return Result.success();
    }

    //    查询全部
    @GetMapping("/selectAll")
    public Result selectAll() {
        List<Equipment> equipmentList = equipmentService.list(new QueryWrapper<Equipment>().orderByDesc("id"));//select * from equipment order by id desc
        return Result.success(equipmentList);
    }

    //    id查询
    @GetMapping("/selectById/{id}")
    public Result selectId(@PathVariable Integer id) {
        Equipment equipment = equipmentService.getById(id);
        return Result.success(equipment);
    }

    //    name查询
    @GetMapping("/selectByName/{name}")
    public Result selectName(@PathVariable String name) {
        Equipment equipment = equipmentService.getByName(name);
        return Result.success(equipment);
    }

    //    分页多条件模糊查询
    //    pageNum当前页码
    //    一页多少个
    @GetMapping("/selectByPage")
    public Result selectByPage(@RequestParam String name,
                               //@RequestParam String qua,
                               @RequestParam Integer pageNum,
                               @RequestParam Integer pageSize) {
        QueryWrapper<Equipment> queryWrapper = new QueryWrapper<Equipment>().orderByDesc("id");
        queryWrapper.like(StrUtil.isNotBlank(name), "name", name);
       // queryWrapper.like(StrUtil.isNotBlank(qua), "qua", qua);
        Page<Equipment> page = equipmentService.page(new Page<>(pageNum, pageSize), queryWrapper);
        return Result.success(page);
    }
}

